package com.yidu.controller;

import com.yidu.entity.SUser;
import com.yidu.service.SUserService;
import com.yidu.utils.BaseMessage;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * (SUser)表控制层
 *
 * @author wanghong
 * @since 2021-07-29 16:30:30
 */
@RestController
@RequestMapping("/sUser")
public class SUserController {
    /**
     * 服务对象
     */
    @Autowired
    private SUserService sUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public SUser selectOne(Integer id) {
        return this.sUserService.queryById(id);
    }

    @PostMapping("/findLogin")
    public Map<String,Object> findLogin(SUser sUser){
        System.out.println(sUser.toString());
        SUser sUser1= sUserService.findNameAndPwd(sUser.getUsername(),sUser.getUserpwd());
        Map<String,Object> map = new HashMap<>();
        if (sUser1!=null){
            map.put("resp",1);
            map.put("msg","登录成功");
            map.put("data",sUser1);
        }else {
            map.put("resp",0);
            map.put("msg","登录失败");
        }
        return map;
    }

    @PostMapping("/checkName")
    public Map<String,Object> checkName(String userName){
        SUser sUser= sUserService.findByName(userName);
        if (sUser!=null){
            return BaseMessage.judge("1","用户名已存在");
        }else {
            return BaseMessage.judge("0","新用户注册");
        }


    }


    @PostMapping("/userAdd")
    public Map<String,Object> userAdd(SUser sUser){
        SUser sUser2 = sUserService.findByName(sUser.getUsername());
        Map<String,Object> map = new HashMap<>();
        if(sUser2!=null){
            map.put("resp", -1);
            map.put("msg", "该用户名已存在");
        }else {
            SUser sUser1 = sUserService.insert(sUser);
            if (sUser1 != null) {
                map.put("resp", 1);
                map.put("msg", "登录成功");
                map.put("data", sUser1);
            } else {
                map.put("resp", 0);
                map.put("msg", "登录失败");
            }
        }
        return map;
    }
}
